"""
    学生管理系统
        1. 数据模型类 StudentModel
            数据：编号 ID ，姓名 nama ， 年龄 age ， 成绩 score
        2. 逻辑控制类StudentManagerController
            数据： __stu_list
            行为： 获取列表 stu_list
                   添加学生 add_student
                   删除学生 re_student
                   修改学生 update_student
                   根据成绩排序 order_by_score
        3. 界面视图类： StudentManagerView
            数据成员：逻辑控制对象 __manager
            行为成员：显示菜单 __display_menu
                      选择菜单 __select_menu
                      输入学生 __input_students
                      输出学生 __output_students
                      删除学生 __re_students
                      修改学生信息 __modify_students
                      按成绩输出学生 __output_students_up

"""

class StudentModel:
    """
    数据模型类 StudentModel
            数据：编号 ID ，姓名 nama ， 年龄 age ， 成绩 score
    """
    def __init__(self, name, age, score, sex, id=0):
        self.__id = id
        self.name = name
        self.age = age
        self.score = score
        self.sex = sex
    
    @property
    def id(self):
        return self.__id
    @id.setter
    def id(self, value):
        self.__id = value
    
    @property
    def name(self):
        return self.__name
    @name.setter
    def name(self, name):
        self.__name = name
    
    @property
    def age(self):
        return self.__age
    @age.setter
    def age(self, value):
        self.__age = value
    
    @property
    def score(self):
        return self.__score
    @score.setter
    def score(self, value):
        self.__score = value
    
    @property
    def sex(self):
        return self.__sex
    @sex.setter
    def sex(self, sex):
        self.__sex = sex
        

class StudentManagerController:
    __stu_id = 100
    def __init__(self):
        self.__stu_list = []
    
    @property
    def stu_list(self):
        """获取列表
        """
        return self.__stu_list
    
    def add_student(self, student):
        """添加学生
        """
        StudentManagerController.__stu_id += 1
        student.id = StudentManagerController.__stu_id
        self.__stu_list.append(student)

    def re_student(self, student):
        """
        删除学生
        """
        for i in self.__stu_list:
            if i.id == student:
                self.__stu_list.remove(i)
                return True
        return False
        
        
    def update_student(self, value):
        """修改学生信息
        """
        for i in self.__stu_list:
            if i.id == value.id:
                i.name = value.name
                i.age = value.age
                i.score = value.score
                i.sex = value.sex
                print("修改完成！")
    
    def stuIDsort(self):
        """
        排序
        """
        for i in range(len(self.__stu_list)-1):
            for j in range(i+1, len(self.__stu_list)):
                if self.__stu_list[i].score > self.__stu_list[j].score:
                    self.__stu_list[i], self.__stu_list[j] = self.__stu_list[j], self.__stu_list[i]
                    


class StudentManagerView:
    """ 显示类，视图模块
    """
    def __init__(self):
        self.__kz = StudentManagerController()
    
    def __display_menu(self):
        """
        显示菜单
        """
        print("1) 添加学生")
        print("2) 显示学生")
        print("3) 删除学生")
        print("4) 修改学生")
        print("5) 按照成绩升序排列学生")
        
    def __select_menu(self):
        item = input("请输入序号：")
        if item == "1":
            self.__input_students() # 添加
        elif item == "2":
            self.__output_students() # 显示
        elif item == "3":
            self.__re_students() # 删除
        elif item == "4":
            self.__modify_students() # 修改
        elif item == "5":
            self.__output_students_up() # 升序
        else:
            print("输入正确的序号！")
    
    def __input_students(self):
        name = input("输入姓名：")
        age = input("输入年龄：")
        score = input("输入成绩：")
        sex = input("输入性别：")
        self.menry = StudentModel(name, age, score, sex) # 实例化一个学生
        self.__kz.add_student(self.menry) # 添加进列表
    
    def __output_students(self):
        for i in self.__kz.stu_list:
            print(i.id, i.name, i.age, i.score, i.sex)
        
    
    def __re_students(self):
        self.__output_students() # 显示，选择
        i = int(input("选择那个学生id进行删除："))
        if self.__kz.re_student(i):
            print("删除成功！")
        else:
            print("删除失败。")
        
    def __modify_students(self):
        self.__output_students() # 显示
        print("选择编号，修改个人信息：")
        name = input("输入姓名：")
        age = input("输入年龄：")
        score = input("输入成绩：")
        sex = input("输入性别：")
        id = int(input("输入选择的编号："))
        self.ls = StudentModel(name, age, score,sex,id)
        self.__kz.update_student(self.ls)
        
        
    def __output_students_up(self):
        self.__kz.stuIDsort()
        self.__output_students() # 显示
    
    
    def main(self):
        while True:
            self.__display_menu()
            self.__select_menu()
            

    

#%% 测试代码
# l = StudentManagerController()
# zs = StudentModel('张三', 22, 88, '男')
# ls = StudentModel('李四', 23, 88, '男')
# wemz = StudentModel('王二麻子', 12, 88, '男')
# st = StudentModel('师太', 29, 88, '女')
# l.add_student(zs)
# l.add_student(ls)
# l.add_student(wemz)
# l.add_student(st)
# l.re_student(101)
# l.update_student(StudentModel('张三三', 22, 88, '男', 101))
# for i in l.stu_list:
#     print(i.id, i.name, i.age, i.score, i.sex)
l = StudentManagerView()
l.main()













